//
//  NSValueAdditions.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 30/08/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "NSValueAdditions.h"
#include <objc/objc-class.h>


@implementation NSValue (HOMAdditions)

+ (id)valueForType:(const char *)objcType value:(void *)value {
	switch (*objcType) {
		case _C_ID:
		case _C_CLASS:
			return [self valueWithNonretainedObject:*((id *)value)];
			
		case _C_SEL:
		case _C_PTR:
			return [self valueWithPointer:value];
			
		case _C_CHARPTR:
			return [NSString stringWithCString:(const char *)value];
			
		case _C_CHR:
			return [NSNumber numberWithChar:*((char *)value)];
			
		case _C_UCHR:
			return [NSNumber numberWithUnsignedChar:*((unsigned char *)value)];
			
		case _C_SHT:
			return [NSNumber numberWithShort:*((short *)value)];
			
		case _C_USHT:
			return [NSNumber numberWithUnsignedShort:*((unsigned short *)value)];
			
		case _C_INT:
			return [NSNumber numberWithInt:*((int *)value)];
			
		case _C_UINT:
			return [NSNumber numberWithUnsignedInt:*((unsigned int *)value)];
			
		case _C_LNG:
			return [NSNumber numberWithLong:*((long *)value)];
			
		case _C_ULNG:
			return [NSNumber numberWithUnsignedLong:*((unsigned long *)value)];
			
		case _C_FLT:
			return [NSNumber numberWithFloat:*((float *)value)];
			
		case _C_DBL:
			return [NSNumber numberWithDouble:*((double *)value)];
			
		case _C_STRUCT_B:
			if (strcmp(objcType, @encode(NSPoint)) == 0)
				return [self valueWithPoint:*((NSPoint *)value)];
			
			if (strcmp(objcType, @encode(NSRange)) == 0)
				return [self valueWithRange:*((NSRange *)value)];
				
			if (strcmp(objcType, @encode(NSSize)) == 0)
				return [self valueWithSize:*((NSSize *)value)];
				
			if (strcmp(objcType, @encode(NSRect)) == 0)
				return [self valueWithRect:*((NSRect *)value)];
			
		default:
			return [self valueWithBytes:value objCType:objcType];
	}
}

@end
